package com.aaa.a1109.demo03;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author 荆豪峰
 * @version 1.0
 */
public class ProxyFactory implements MethodInterceptor {
    private Object target;
    public ProxyFactory(Object target){
        this.target = target;
    }
    public Object getInstance() {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        DZH();
        //调用被代理对象的方法
        Object result = method.invoke(target, objects);
        DX();
        return result;
    }
    public void DZH() {
        System.out.println("打招呼");
    }

    public void DX() {
        System.out.println("答谢");
    }
}
